package collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class LIstDemo {
    public static void main(String[] args) {
        List<String>list = new ArrayList<>();
        list.add("one");
        list.add("tow");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("one");
        System.out.println("list"+list);

        //E get(int index)获取指定下标所对应的元素
        String e = list.get(2);//获取第三个元素
        System.out.println(e);

        for (int i = 0;i< list.size();i++){
            System.out.println(list.get(i));
        }
        for (String s : list){
            System.out.println(s);
        }
        Iterator<String>it = list.listIterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        System.out.println("-----------");
        //list.set(2,"six");//将list中下标为2的元素设置为six---常规用法
        String old = list.set(2,"six");//将list中下标为2的元素设置为six，同时将原数据返回给old
        System.out.println(old);
        System.out.println("list"+list);


        //E set(int index,E e):将给定元素设置到制定位置，返回被删除的元素
        //list.remove(2);//删除下标为2的元素---常规用法
        String s = list.remove(2);
        System.out.println(s);
        System.out.println("list"+list);


        //void add(int index,E e):将给定元素e添加到index所指定的位置，相当于插入操作
        list.add(3,"seven");
        System.out.println("list"+list);


    }
}
